from os import close
import random
from numpy import array
from PIL import Image, ImageDraw, ImageFont

def plot_one_box(x, img, color=(0, 0, 180), label=None, line_thickness=None):
    # x => top, right, bottom, left
    # Plots one bounding box on image img
    tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1  # line/font thickness
    xy = (int(x[3]), int(x[0])), (int(x[1]), int(x[2]))
    img = Image.fromarray(img)
    draw = ImageDraw.Draw(img)
    fontsize = int((x[2]-x[0])*0.1)
    fontStyle = ImageFont.truetype('font/simsun.ttc', fontsize, encoding='utf-8')
    draw.rectangle(xy, outline=color, width=line_thickness)
    draw.text((int(x[3]), int(x[0])-fontsize), label, color, fontStyle)
    
    return array(img)